package com.example.reggie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.reggie.common.BaseContext;
import com.example.reggie.common.R;
import com.example.reggie.entity.ShoppingCart;
import com.example.reggie.mapper.ShoppingCartMapper;
import com.example.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @Transactional
    public ShoppingCart saveWithAll(ShoppingCart shoppingCart) {
        //用户的id 设置user_id
        Long userId= BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //有记录,判断是dish还是setmeal dish->true setmeal->false
        boolean flage=false;
        flage=shoppingCart.getDishId()!=null;
        Long randId=flage?shoppingCart.getDishId():shoppingCart.getSetmealId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);

        if (flage){
            // 如果是菜品:select * from reggie where user_id = ? and dish_id = ? and dish_flavor = ? 存在则更新: amount = amount + 1 ;
            queryWrapper.eq(ShoppingCart::getDishId,randId);
            queryWrapper.eq(ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor());
            ShoppingCart shoppingCart1 = this.getOne(queryWrapper);
            //无记录则创建 有则更新
            if (shoppingCart1==null){
                this.save(shoppingCart);
            }else {
                shoppingCart.setId(shoppingCart1.getId());
                shoppingCart.setNumber(shoppingCart1.getNumber()+1);
                this.updateById(shoppingCart);
            }
        }else {
            // 如果是套餐:select * from reggie where user_id = ? and setmeal_id = ? 存在则更新: amount = amount + 1 ;
            queryWrapper.eq(ShoppingCart::getSetmealId,randId);
            ShoppingCart shoppingCart1 = this.getOne(queryWrapper);
            if (shoppingCart1==null){
                this.save(shoppingCart);
            }else{
                shoppingCart.setId(shoppingCart1.getId());
                shoppingCart.setNumber(shoppingCart1.getNumber()+1);
                this.updateById(shoppingCart);
            }
        }
        return shoppingCart;
    }

    /**
     * 减少购物车
     * @param
     * @return
     */
    @Transactional
    public ShoppingCart subWithAll(ShoppingCart shoppingCart) {
        //用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //菜品或套餐id
        Long randId=shoppingCart.getDishId()==null?shoppingCart.getSetmealId():shoppingCart.getDishId();
        boolean flage=shoppingCart.getDishId()==null?false:true;
        log.info("菜品或者套餐:{} id:{}",flage,randId);
        //是菜品
        if (flage) {
            LambdaQueryWrapper<ShoppingCart>queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(ShoppingCart::getUserId,userId);
            queryWrapper.eq(ShoppingCart::getDishId,randId);
            queryWrapper.eq(ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor());
            ShoppingCart res = this.getOne(queryWrapper);
            if (shoppingCart.getNumber()==1){
                this.remove(queryWrapper);
                return shoppingCart;
            }
            //update shopping_cart set number = number - 1 where user_id = ? and dish_id = ? and dish_flavor = ? ;
            res.setNumber(shoppingCart.getNumber()-1);
            this.updateById(res);
            return res;
        }else {
            //套餐
            LambdaQueryWrapper<ShoppingCart>queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(ShoppingCart::getUserId,userId);
            queryWrapper.eq(ShoppingCart::getSetmealId,randId);
            ShoppingCart one = this.getOne(queryWrapper);
            if (one.getNumber()==1){
                this.removeById(one.getId());
                return one;
            }
            one.setNumber(one.getNumber()-1);
            this.updateById(one);
            return one;
        }
    }

}
